#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
baseWindow::baseWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    ,set_form(nullptr)
    ,edit_form(nullptr)
{
    ui->setupUi(this);
    InitWindow();
    InitSignal();
}

baseWindow::~baseWindow()
{
    delete ui;
}

void baseWindow::InitSignal()
{
    GlobalSignalMgr::get_instance()->attach(SignalType::SIG_RELAYOUT_WINDOW, this, SLOT(layout_resize()));
    GlobalSignalMgr::get_instance()->attach(SignalType::SIG_FILEBROWSER_SHOWORHID_VOID, this, SLOT(on_actionfile_browser_triggered()));
    GlobalSignalMgr::get_instance()->attach(SignalType::SIG_SEARCH_FORM_OPEN_VOID, this, SLOT(on_actionSearch_triggered()));
}

void baseWindow::InitWindow()
{
    setWindowTitle(tr("Useless Note"));
    setWindowIcon(QIcon(QString(":/images/app_main.png")));
    ui->menuPreference->setTitle(tr("Preference"));
    ui->actionOpen_Folder->setText(tr("Open Folder"));
    ui->actionExit->setText(tr("Exit"));
    ui->actionSettings->setText(tr("Settings"));
    ui->actionSearch->setText(tr("Search (Ctrl+F)"));
    status_bar = new Status_bar(this);

    set_floder = new set_Floder(this);
    set_form = new user_settings();
    edit_form = new editer_tab_widget(this);
    file_browser_form = new File_Browser(this);
    search_form = new searcher(this);
    search_form->setWindowIcon(QIcon(QString(":/images/search.png")));
    int x = Config_file().get("window", "point_x").toInt();
    int y = Config_file().get("window", "point_y").toInt();
    int w = Config_file().get("window", "width").toInt();
    int h = Config_file().get("window", "height").toInt();
    if (w|y|w|h){
        move(x,y);
        resize(w, h);
        layout_resize();
    }
}

void baseWindow::layout_resize()
{
    int start_x = 0;
    int start_y = 0+ ui->menubar->height();
    int window_w = rect().width();
    int window_h = rect().height() - ui->menubar->height();
    qInfo(QString("step1 x%1,y%2,w%3,h%4").arg(start_x).arg(start_y).arg(window_w).arg(window_h).toStdString().c_str());
    status_bar->resize(window_w, 20);
    status_bar->move(start_x, start_y + window_h - status_bar->height());
    window_h -= status_bar->height();
    if (file_browser_form && !file_browser_form->isHidden()){
        file_browser_form->move(start_x, start_y);
        int use_width = (int) (window_w / 3);
        qInfo(QString("floder x%1,y%2,w%3,h%4").arg(start_x).arg(start_y).arg(use_width).arg(window_h).toStdString().c_str());
        start_x += use_width;
        file_browser_form->resize(use_width, window_h);
    }
    if (edit_form && !edit_form->isHidden()){
        edit_form->move(start_x, start_y);
        int use_width = window_w - start_x;
        qInfo(QString("editer x%1,y%2,w%3,h%4").arg(start_x).arg(start_y).arg(use_width).arg(window_h).toStdString().c_str());
        start_x += use_width;
        edit_form->resize(use_width, window_h);
    }
}

void baseWindow::keyPressEvent(QKeyEvent *event)
{
    is_key_event_emit(event);
}

void baseWindow::closeEvent(QCloseEvent *event)
{
    Config_file().set("window", "point_x", x());
    Config_file().set("window", "point_y", y());
    Config_file().set("window", "width", width());
    Config_file().set("window", "height", height());
    qInfo("quit UselessNote");
}

void baseWindow::on_actionOpen_Folder_triggered()
{
    GlobalSignalMgr::get_instance()->notify(SignalType::SIG_SET_STATUS, "open floder set window");
    set_floder->show();

}

void baseWindow::on_actionSettings_triggered()
{

    set_form->show();
}

void baseWindow::on_actionExit_triggered()
{
    exit(0);
}

void baseWindow::set_status_bar_text(QVariant& data)
{
}

void baseWindow::on_actionfile_browser_triggered(){

    if(file_browser_form->isHidden()){
        file_browser_form->show();
    }
    else{
        file_browser_form->hide();
    }
    layout_resize();
}

void baseWindow::on_actionSave_SignalFile_triggered()
{
    GlobalSignalMgr::get_instance()->notify(SignalType::SIG_FILE_SAVE_SINGLE_VOID);
}

void baseWindow::on_actionSearch_triggered()
{
    if(search_form->isHidden()){
        search_form->show();
    }
    else{
        search_form->hide();
    }
}

void baseWindow::showEvent(QShowEvent *event)
{
    layout_resize();
}

void baseWindow::resizeEvent(QResizeEvent *event)
{
    layout_resize();
}


